package tae.com.Currency;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Currency2 extends Activity {
	final double EURO2USD = 1.399;
	final double COLON2USD = 0.001736;
	
	//GUI
	Button btnConvert;
	Button btnClear;
	EditText txtUSDollars;
	EditText txtEuros;
	EditText txtColones;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relative);
        
        //bind
        txtUSDollars = (EditText)findViewById(R.id.txtUSDollars);
        txtEuros = (EditText)findViewById(R.id.txtEuros);
        txtColones = (EditText)findViewById(R.id.txtColones);
        
        //attach click
        btnClear = (Button)findViewById(R.id.btnClear);
        btnClear.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				txtColones.setText("");
				txtEuros.setText("");
				txtUSDollars.setText("");
			}
		});
        
        //do the conversion from USD
        btnConvert = (Button) findViewById(R.id.btnConvert);
        btnConvert.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					String usdStr = txtUSDollars.getText().toString();
					double usd = Double.parseDouble(usdStr);
					String euros = String.valueOf(usd / EURO2USD);
					String colones = String.valueOf(usd / COLON2USD);
					txtEuros.setText(euros);
					txtColones.setText(colones);
					
				} catch(Exception e) {
					Toast.makeText(v.getContext(), "Invalid data - try again", Toast.LENGTH_LONG).show();
				}
			}
		});
    }
}